<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择班级">
          <el-option
            v-for="dict in classList"
            :label="dict.name"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生" prop="studentId">
        <el-select v-model="queryParams.studentId" placeholder="请选择学生">
          <el-option
            v-for="dict in stuList"
            :label="dict.name"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问题类型" prop="impartType">
        <el-select v-model="queryParams.impartType" placeholder="请选择问题类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_student_question"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="扣分" prop="discount">
        <el-input
          v-model="queryParams.discount"
          placeholder="请输入扣分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="creationtime">
        <el-date-picker clearable
                        v-model="queryParams.creationtime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="跟踪老师" prop="teachername">
        <el-select v-model="queryParams.teachername" placeholder="请选择跟踪老师">
          <el-option
            v-for="dict in teachList"
            :label="dict.name"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="跟踪次数" prop="stalkeCount">
        <el-input
          v-model="queryParams.stalkeCount"
          placeholder="请输入跟踪次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!--搜索条件栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teaching:stalke:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teaching:stalke:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teaching:stalke:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:stalke:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<!--  展示列表  -->
    <el-table v-loading="loading" :data="stalkeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="index" prop="id"/>
      <el-table-column label="班级" align="center" prop="bblClass.name"/>
      <el-table-column label="学生" align="center" prop="student.name"/>
      <el-table-column label="问题类型" align="center" prop="impartType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_student_question" :value="scope.row.impartType"/>
        </template>
      </el-table-column>
      <el-table-column label="扣分" align="center" prop="discount"/>
      <el-table-column label="创建时间" align="center" prop="creationtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creationtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="跟踪老师" align="center" prop="teacher.name"/>
      <el-table-column label="跟踪次数" align="center" prop="stalkeCount"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teaching:stalke:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teaching:stalke:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老师跟踪对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班级" prop="classes">
          <el-select v-model="form.classId" placeholder="请选择班级">
            <el-option
              v-for="dict in classList"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生" prop="studentsname">
          <el-select v-model="form.studentId" placeholder="请选择学生">
            <el-option
              v-for="dict in stuList"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题类型" prop="impartType">
          <el-select v-model="form.impartType" placeholder="请选择问题类型">
            <el-option
              v-for="dict in dict.type.sys_student_question"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="扣除分数" prop="discount">
          <el-input v-model="form.discount" placeholder="请输入扣分"/>
        </el-form-item>
        <el-form-item label="发生时间" prop="creationtime">
          <el-date-picker clearable
                          v-model="form.creationtime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="问题描述" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStalke,
  getStalke,
  delStalke,
  addStalke,
  updateStalke,
  getStu,
  getteacher,
  getClass
} from "@/api/teaching/stalke";

export default {
  name: "Stalke",
  dicts: ['sys_student_question'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师跟踪
      // 表格数据
      stalkeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classes: null,
        studentsname: null,
        impartType: null,
        discount: null,
        creationtime: null,
        teachername: null,
        stalkeCount: null,
        classId: null,
        studentId: null,
        teacherId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 所有学生
      stuList:[],
      // 所有老师
      teachList:[],
      // 所有班级
      classList:[],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询老师跟踪列表 */
    getList() {
      this.loading = true;
      listStalke(this.queryParams).then(response => {
        this.stalkeList = response.rows;
        console.log(JSON.stringify(response.rows))
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classes: null,
        studentsname: null,
        impartType: null,
        discount: null,
        creationtime: null,
        teachername: null,
        stalkeCount: null,
        remark: null,
        classId: null,
        studentId: null,
        teacherId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生问题记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStalke(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老师跟踪";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStalke(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStalke(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除老师跟踪编号为"' + ids + '"的数据项？').then(function () {
        return delStalke(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/stalke/export', {
        ...this.queryParams
      }, `stalke_${new Date().getTime()}.xlsx`)
    },
    /** 获得学生的全部*/
    getstuList(){
      getStu().then(response =>{
        this.stuList = response.rows
      })
    },
    /** 获得老师的全部*/
    getTeacherList(){
      getteacher().then(response =>{
        this.teachList = response.rows
      })
    },
    /** 获得班级的全部*/
    getclassList(){
      getClass().then(response =>{
        this.classList = response.rows
      })
    }
  },
  mounted() {
    this.getstuList();
    this.getclassList();
    this.getTeacherList();
  }
};
</script>
